package com.leyou.item.service;

import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryService{

    @Autowired
    CategoryMapper cm;


    public List<Category> queryCategoriesByPid(Long pid) {
        Category c = new Category();
        c.setParentId(pid);
        return cm.select(c);
    }

    public List<Category> queryCategoriesByBid(Integer bid) {
        // 1.根据bid去tb_category_brand表中查询，得到category的id
        // select cid from tb_category_brand where bid = #{bid}
        // 2.根据得到的id，再去tb_category表中查看对应的类对象
        // select * from tb_category where id in cids
        return cm.queryCategoriesByBid(bid);

    }

    public List<String> getNamesByIds(List<Long> ids) {
        List<Category> categories = cm.selectByIdList(ids);
        List<String> names = new ArrayList<>();
        categories.forEach(c->{
            names.add(c.getName());
        });
        return names;
    }
}
